package ServerSocket2;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

/**
 * Created by 旧 on 2018/8/11.
 */
public class Cliet {
    public static void main(String[] args) throws IOException {
        Scanner scanner=new Scanner(System.in);
        //1.创建客户端
        Socket socket=new Socket("192.168.0.176",8000);
        System.out.println("客户端启动成功，连接服务端8000成功！");
        //2.创建读取流
        InputStream input = socket.getInputStream();
        OutputStream out = socket.getOutputStream();
            //2.1 创建一个用户对象
        User user=new User("哪吒");
        ObjectOutputStream outputStream=new ObjectOutputStream(out);
        outputStream.writeObject(user);


        //3.创建一个 集体高效的读取流 BufferedReader
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(input));
        while (true){
            //4.向服务端发一个消息
            System.out.println("请输入发送到服务端的内容");
            String info=scanner.next();
            out.write( (info+"\n").getBytes() );
            if (info.equals("bye")) {
                break;
            }
            //5.读取服务端发回来的内容
            String s=bufferedReader.readLine();
            System.out.println("服务端发回来的内容是："+s);
        }
        //6.关闭
        bufferedReader.close();
        out.close();
        socket.close();
    }
}
